<?php
namespace app\data_magic\controller;

use cmf\controller\HomeBaseController;
use app\data_magic\logic\Structure;
use app\data_magic\logic\Scene;
use app\data_magic\logic\Database;

/**
 * 管理页面
 */
class AdminController extends HomeBaseController
{
    public function index()
    {
        $html="<p><a href='".url("showDatabaseTableList")."'>数据表列表</a></p>";
        $html.="<p><a href='".url("showStructureList")."'>结构列表</a></p>";
        $html.="<p><a href='".url("showSceneList")."'>场景列表</a></p>";
        return $html;
    }
    public function showDatabaseTableList()
    {
        $database=new Database();
        $list=$database->getTableList();
        $html="<ul>";
        foreach($list as $item){
            $html.="<li>
                <pre>".json_encode($item)."</pre>
                <p><a href='".url("showDatabaseTableDetail",["table_name"=>$item["TABLE_NAME"]])."'>显示详细信息</a></p>
                <p><a href='".url("createStructureWithDatabaseTable",["table_name"=>$item["TABLE_NAME"]])."'>从此数据表创建结构</a></p>
            </li>";
        }
        $html."</ul>";
        return $html;
    }
    public function showDatabaseTableDetail()
    {
        $table_name=input("table_name");
        if(!$table_name){
            $this->error("数据表名称不能为空");
        }
        $database=new Database();
        list($table,$columns)=$database->getDetail($table_name);
        
        $html="<pre>".json_encode($table,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT)."</pre>";
        $html.="<ul>";
        foreach($columns as $item){
            $html.="<li><pre>".json_encode($item)."</pre></li>";
        }
        $html."</ul>";
        $html.="<p><a href='".url("createStructureWithDatabaseTable",["table_name"=>$table["TABLE_NAME"]])."'>创建结构</a></p>";
        $html.="<p><a href='".url("showCreateStructurePage",["table_name"=>$table["TABLE_NAME"]])."'>显示创建结构页面</a></p>";
        return $html;
    }
    public function createStructureWithDatabaseTable()
    {
        $table_name=input("table_name");
        if(!$table_name){
            $this->error("数据表名称不能为空");
        }
        $database=new Database();
        $data=$database->transToStructure($table_name);
        $structure=new Structure();
        $result=$structure->createStructure($data["table"],$data["fields"],$data["flag"]);
        if(!$result){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }

    public function showCreateStructurePage()
    {
        $table_name=input("table_name");
        if($table_name){
            $structure=new Structure();
            $data=$structure->transDatabaseTableToStructure($table_name);
            $this->assign($data);
        }
        return $this->fetch("create");
    }
    public function submitCreateStructureAction()
    {
        $table=input("table");
        $fields=[];
        $fields_name=input("fields_name");
        $fields_alias=input("fields_alias");
        $fields_title=input("fields_title");
        $fields_describe=input("fields_describe");
        $fields_data_type=input("fields_data_type");
        $fields_max_length=input("fields_max_length");
        $fields_is_nullable=input("fields_is_nullable");
        $fields_type=input("fields_type");
        $fields_type_params=input("fields_type_params");
        foreach($fields_name as $index=>$name){
            $fields[]=[
                "name"=>$name,
                "alias"=>$fields_alias[$index],
                "title"=>$fields_title[$index],
                "describe"=>$fields_describe[$index],
                "data_type"=>$fields_data_type[$index],
                "max_length"=>$fields_max_length[$index],
                "is_nullable"=>$fields_is_nullable[$index],
                "type"=>$fields_type[$index],
                "type_params"=>$fields_type_params[$index],
            ];
        }
        $structure=new Structure();
        $result=$structure->createStructure($table,$fields);
        if(!$result){
            $this->error("操作失败");
        }
        $this->success("操作成功");
    }
    public function showStructureList()
    {
        $structure=new Structure();
        $list=$structure->getStructureList();
        $html="<ul>";
        foreach($list as $item){
            $html.="<li>
                <p>".json_encode($item)."</p>
                <p><a href='".url("showStructureDetail",["table_id"=>$item["id"]])."'>显示详细信息</a></p>
                <p><a href='".url("data/showListPage",["table_id"=>$item["id"]])."'>显示数据</a></p>
            </li>";
        }
        $html."</ul>";
        return $html;
    }
    public function showStructureDetail()
    {
        $table_id=input("table_id");
        if($table_id){
            $structure=new Structure();
            $data=$structure->getStructureDetail($table_id);
            $this->assign($data);
            $scenes=$structure->getSceneList(["table_id"=>$table_id]);
            $this->assign("scenes",$scenes);
            $actions=$structure->getActionList(["table_id"=>$table_id]);
            $this->assign("actions",$actions);
            $filters=$structure->getFilterList(["table_id"=>$table_id]);
            $this->assign("filters",$filters);
        }
        return $this->fetch("structure_detail");
    }
    public function showEditStructurePage()
    {
        $table_id=input("table_id");
        if(!$table_id){
            $this->error("id不能为空");
        }
        $structure=new Structure();
        $data=$structure->getStructureDetail($table_id);
        $this->assign($data);
        $scenes=$structure->getSceneList(["table_id"=>$table_id]);
        $this->assign("scenes",$scenes);
        $actions=$structure->getActionList(["table_id"=>$table_id]);
        $this->assign("actions",$actions);
        return $this->fetch("structure_detail");
    }
    public function submitEditStructureTableAction()
    {
        $table_id=input("table_id");
        if(!$table_id){
            $this->error("id不能为空");
        }
        $structure=new Structure();
        $result=$structure->updateStructureTable($table_id,$_REQUEST);
        $this->success("操作成功");
    }
    public function showEditStructureFieldPage()
    {
        $field_id=input("field_id");
        if(!$field_id){
            $this->error("id不能为空");
        }
        $structure=new Structure();
        $field=$structure->getStructureFieldDetail($field_id);
        $this->assign("field",$field);
        return $this->fetch("field_detail");
    }
    public function submitEditStructureFieldAction()
    {
        $field_id=input("field_id");
        if(!$field_id){
            $this->error("id不能为空");
        }
        $structure=new Structure();
        $result=$structure->updateStructureField($field_id,$_REQUEST);
        $this->success("操作成功");
    }

    public function showCreateScenePage()
    {
        $table_id=input("table_id");
        if(!$table_id){
            $this->error("id不能为空");
        }
        $this->assign("table_id",$table_id);
        return $this->fetch("scene_create");
    }
    public function submitCreateSceneAction()
    {
        $structure=new Structure();
        $result=$structure->createScene($_REQUEST);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    public function submitRemoveSceneAction()
    {
        $scene_id=input("scene_id");
        $structure=new Structure();
        $result=$structure->removeScene($scene_id);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    public function showEditScenePage()
    {
        $scene_id=input("scene_id");
        if(!$scene_id){
            $this->error("id不能为空");
        }
        $structure=new Structure();
        $scene=$structure->getSceneDetail($scene_id);
        $this->assign("scene",$scene);
        return $this->fetch("scene_detail");
    }
    public function submitEditSceneAction()
    {
        $scene_id=input("scene_id");
        if(!$scene_id){
            $this->error("id不能为空");
        }
        $structure=new Structure();
        $result=$structure->updateScene($scene_id,$_REQUEST);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    public function submitEditSceneFieldAction()
    {
        $scene_id=input("scene_id");
        $data=$_REQUEST;
        $this->getListFromCheckboxList($data,"readable");
        $this->getListFromCheckboxList($data,"writable");
        $this->getListFromCheckboxList($data,"required");
        $structure=new Structure();
        $result=$structure->updateScene($scene_id,$data);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    /**
     * 将Checkbox列表的值转换成id字符串
     */
    public function getListFromCheckboxList(&$data,$name)
    {
        if(isset($data[$name])){
            $list=[];
            foreach($data[$name] as $index => $value){
                if($value){
                    $list[]=$index;
                }
            }
            $data[$name]=implode(",",$list);
        }
    }
    public function showSceneList()
    {
        $structure=new Structure();
        $list=$structure->getSceneList();
        $html="<ul>";
        foreach($list as $item){
            $html.="<li>
                <p>".json_encode($item)."</p>
                <p><a href='".url("crud/showListPage",["scene_id"=>$item["id"]])."'>显示数据</a></p>
            </li>";
        }
        $html."</ul>";
        return $html;
    }

    public function showCreateFilterPage()
    {
        $table_id=input("table_id");
        if(!$table_id){
            $this->error("表id不能为空");
        }
        $this->assign("table_id",$table_id);
        return $this->fetch("filter_create");
    }
    public function submitCreateFilterAction()
    {
        $structure=new Structure();
        $result=$structure->createFilter($_REQUEST);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    public function submitRemoveFilterAction()
    {
        $filter_id=input("filter_id");
        $structure=new Structure();
        $result=$structure->removeFilter($filter_id);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    public function showEditFilterPage()
    {
        $filter_id=input("filter_id");
        if(!$filter_id){
            $this->error("id不能为空");
        }
        $structure=new Structure();
        $filter=$structure->getFilterDetail($filter_id);
        $this->assign("filter",$filter);
        return $this->fetch("filter_detail");
    }
    public function submitEditFilterAction()
    {
        $filter_id=input("filter_id");
        if(!$filter_id){
            $this->error("id不能为空");
        }
        $structure=new Structure();
        $result=$structure->updateFilter($filter_id,$_REQUEST);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }

    public function execCreateUserActionGroup()
    {
        $table_id=input("table_id");
        $scene_id=input("scene_id");
        if(!$table_id || !$scene_id){
            $this->error("表id不能为空");
        }
        $structure=new Structure();
        if(!$structure->createUserActionGroup($table_id,$scene_id)){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    public function execCreateOwnerActionGroup()
    {
        $table_id=input("table_id");
        $scene_id=input("scene_id");
        if(!$table_id || !$scene_id){
            $this->error("表id不能为空");
        }
        $structure=new Structure();
        if(!$structure->createOwnerActionGroup($table_id,$scene_id)){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    public function showCreateActionPage()
    {
        $table_id=input("table_id");
        $scene_id=input("scene_id");
        if(!$table_id || !$scene_id){
            $this->error("表id不能为空");
        }
        $this->assign("table_id",$table_id);
        $this->assign("scene_id",$scene_id);
        return $this->fetch("action_create");
    }
    public function submitCreateActionAction()
    {
        $structure=new Structure();
        $result=$structure->createAction($_REQUEST);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    public function submitRemoveActionAction()
    {
        $action_id=input("action_id");
        $structure=new Structure();
        $result=$structure->removeAction($action_id);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    public function showEditActionPage()
    {
        $action_id=input("action_id");
        if(!$action_id){
            $this->error("id不能为空");
        }
        $structure=new Structure();
        $action=$structure->getActionDetail($action_id);
        $this->assign("action",$action);
        return $this->fetch("action_detail");
    }
    public function submitEditActionAction()
    {
        $action_id=input("action_id");
        if(!$action_id){
            $this->error("id不能为空");
        }
        $structure=new Structure();
        $result=$structure->updateAction($action_id,$_REQUEST);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    public function submitEditActionBindAction()
    {
        $action_id=input("action_id");
        $data=$_REQUEST;
        $structure=new Structure();
        $result=$structure->updateAction($action_id,["bind_action"=>$data["bind_action"]]);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    public function submitEditActionLinkAction()
    {
        $action_id=input("action_id");
        $data=$_REQUEST;
        $this->getListFromCheckboxList($data,"link_action");
        $structure=new Structure();
        $result=$structure->updateAction($action_id,$data);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
    public function submitEditActionFilterAction()
    {
        $action_id=input("action_id");
        $data=$_REQUEST;
        $this->getListFromCheckboxList($data,"bind_filter");
        $structure=new Structure();
        $result=$structure->updateAction($action_id,$data);
        if($result===false){
            $this->error($structure->getError());
        }
        $this->success("操作成功");
    }
}
